layui.use(['table', 'admin', 'ax', 'func','laydate'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var func = layui.func;
    var laydate = layui.laydate;


    var lastday = 3;

    //当前月份
    var thisdate = new Date();
    if(thisdate.getDate()<lastday){
        thisdate.setMonth(thisdate.getMonth()-1);
    }
    let monthNum = thisdate.getMonth() + 1;
    const month = thisdate.getFullYear() + "-" + (monthNum < 10 ? "0" + monthNum : monthNum);

    //渲染时间选择框
    laydate.render({
        elem: "#month",
        value: month,
        type: 'month',
        tiger: 'click'
    });

    //获取最新日期
    function date() {
        $.ajax({
            type: "post",
            url: "/btyRbsc/selectDate",
            datatype: "json",
            async: false,
            success: function (data) {
                $("#latest").val(data);
                let substr = data.substring(data.length - 13);
                let strings = substr.split(" ");
                let day = strings[0];
                let time = strings[1];
                if (time < 12) {
                    day -= 1;
                }
                $("#day").val(day);
                for (let i = 1; i <= day; i++) {
                    $("#tr").append("<th class='de'>" + i + "</th>");
                }
            }
        });
    }


    //考勤期间判断,默认在非考勤期间
    var buttonValid = false;


    /**
     * 出勤_月度员工档案信息表管理
     */
    var AttendEmployeeMonth = {
        tableId: "attendEmployeeMonthTable"
    };

    /**
     * 初始化表格的列
     */
    AttendEmployeeMonth.initColumn = function () {
        return [[
            // {type: 'checkbox'},
            {field: 'id', hide: true, title: 'id(主键)', hide: true, sort: false},
            {field: 'attendMonth', sort: false, title: '考勤月份',width: 90, fixed: true, style: 'text-align: left'},
            {field: 'dm', sort: false, title: '店名',width: 130, fixed: true, style: 'text-align: left'},
            {field: 'xm', sort: false, title: '姓名',width: 80, fixed: true, style: 'text-align: left'},
            {field: 'tel', sort: false, title: '联系方式',width: 100, fixed: true, style: 'text-align: left'},

            {field: 'synchronousMonth', sort: false, title: '同步月份'},
            {field: 'btyArchicesId', sort: false, title: '员工身份表id'},
            {field: 'sfz', sort: false, title: '身份证'},
            {field: 'zw', sort: false, title: '职位',width:70},
            {field: 'rq', sort: false, title: '日期'},
            {field: 'yhk', sort: false, title: '银行卡号'},
            {field: 'bz', sort: false, title: '备注'},
            {field: 'qy', sort: false, title: '区域'},
            {field: 'eqy', sort: false, title: '二级区域'},
            {field: 'zt', sort: false, title: '人事表是否发工资',templet: function(d){
                return d.zt==1?'否':'是'
              }},
            {field: 'gz', sort: false, title: '工资'},
            {field: 'gw', sort: false, title: '岗位工资'},
            {field: 'openid', sort: false, title: 'openid', hide: true},
            {field: 'bank', sort: false, title: '银行'},
            {field: 'hh', sort: false, title: '行号'},
            {field: 'cardName', sort: false, title: '卡片名称'},
            {field: 'yhkStatus', sort: false, title: '银行卡状态'},
            {field: 'attendanceDays', sort: false, title: '出勤天数'},
            {field: 'holidayDays', sort: false, title: '公休天数'},
            {field: 'shouldPay', sort: false, title: '应发工资'},
            {field: 'payWage', sort: false, title: '是否发工资'},
            {field: 'managerNote', sort: false, title: '店长备注'},
            {field: 'synchronousStatus', sort: false, title: '状态'},
            {field: 'ztUpdate', sort: false, title: '在职状态修改', hide: true},
            {field: 'attendAuditId', sort: false, title: '督导审核状态表_id'},
            {field: 'ztNew', sort: false, title: '状态'},
            {field: 'dmOld', sort: false, title: '调店之前店名'},
            {field: 'bh', sort: false, title: '编号'},
            {field: 'btyArchicesNewEmp', sort: false, title: '人事表新员工',templet: function(d){
                return d.btyArchicesNewEmp==1?'是':'否'
              }},
            {field: 'dingdingNewEmp', sort: false, title: '钉钉表新员工',templet: function(d){
                return d.dingdingNewEmp=='T'?'是':'否'
              }},
            {field: 'zwOld', sort: false, title: '曾用职位'},
            // {align: 'center', toolbar: '#tableBar', title: '操作'}
            {align: 'center',width: 100, toolbar: '#tableBar', title: '操作', fixed: "right", style: 'text-align: right'}
        ]];
    };


    /**
     * 获取是否允许考勤
     *
     * @param 获取是否允许考勤
     */
    AttendEmployeeMonth.getCreateAttendButton = function () {
        $.ajax({
            type: "post",
            url: "/attendAudit/getCreateAttendButton",
            datatype: "json",
            async: false,
            success: function (data) {
                if(data){
                    if(data.data.buttonValid == "0"){
                        buttonValid = true;
                        window.buttonValid = true;
                        attendMonthValid = data.data.attendMonth.replace("-","");
                        window.attendMonthValid = data.data.attendMonth.replace("-","");

                    }else{
                        buttonValid = false;
                        window.buttonValid = false;
                        attendMonthValid = data.data.attendMonth.replace("-","");
                        window.attendMonthValid = data.data.attendMonth.replace("-","");
                    }
                }
                console.log(data);
            }
        });
    };
    /**
     * 点击查询按钮
     */
    AttendEmployeeMonth.search = function () {
        var queryData = {};
        var attendMonth= $("#month").val();
        if(attendMonth){
            queryData['attendMonth'] = attendMonth.replace("-","");
        }else{
            //当前月份
            var thisdate = new Date();
            if(thisdate.getDate()<lastday){
                thisdate.setMonth(thisdate.getMonth()-1);
            }
            let monthNum = thisdate.getMonth() + 1;
            const month = thisdate.getFullYear() + "-" + (monthNum < 10 ? "0" + monthNum : monthNum);
            queryData['attendMonth'] = month.replace("-","");
            $("#month").val(month);
        }
        queryData['dm'] = $("#dm").val();
        queryData['xm'] = $("#xm").val();
        queryData['tel'] = $("#tel").val();
        table.reload(AttendEmployeeMonth.tableId, {
            where: queryData, page: {curr: 1}
        });
    };

    /**
     * 跳转到添加页面
     */
    AttendEmployeeMonth.jumpAddPage = function () {
        window.location.href = Feng.ctxPath + '/attendEmployeeMonth/add'
    };

    /**
    * 跳转到编辑页面
    *
    * @param data 点击按钮时候的行数据
    */
    AttendEmployeeMonth.jumpEditPage = function (data) {
        window.location.href = Feng.ctxPath + '/attendEmployeeMonth/edit?id=' + data.id
    };

    /**
     * 导出excel按钮
     */
    AttendEmployeeMonth.exportExcel = function () {
        var checkRows = table.checkStatus(AttendEmployeeMonth.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("请选择要导出的数据");
        } else {
            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        }
    };

    /**
     * 点击删除
     *
     * @param data 点击按钮时候的行数据
     */
    AttendEmployeeMonth.onDeleteItem = function (data) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/attendEmployeeMonth/delete", function (data) {
                Feng.success("删除成功!");
                table.reload(AttendEmployeeMonth.tableId);
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("id", data.id);
            ajax.start();
        };
        Feng.confirm("是否删除?", operation);
    };


    /**
     * 点击删除
     *
     * @param data 点击按钮时候的行数据
     */
    AttendEmployeeMonth.onSynchronousNameItem = function (data) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/attendEmployeeMonth/synchronousNameById", function (data) {
                if(data.success == true){
                    Feng.success("同步成功!");
                    table.reload(AttendEmployeeMonth.tableId);
                }else{
                    Feng.error(data.data.message);
                }
            }, function (data) {
                Feng.error("同步失败!" + data.responseJSON.message + "!");
            });
            ajax.set("id", data.id);
            ajax.start();
        };
        Feng.confirm("是否同步人员?", operation);
    };


    // 渲染表格
    var tableResult = table.render({
        elem: '#' + AttendEmployeeMonth.tableId,
        url: Feng.ctxPath + '/attendEmployeeMonth/list',
        page: true,
        limit:20,
        height: "full-59",
        cellMinWidth: 100,
        cols: AttendEmployeeMonth.initColumn()
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        AttendEmployeeMonth.search();
    });
    // 重置按钮事件
    $('#btnReset').click(function () {
        $('#dm').val('');
        $('#xm').val('');
        $('#tel').val('');
        $("#month").val(month);
        AttendEmployeeMonth.search();
    });

    // 添加按钮点击事件
    $('#btnAdd').click(function () {

    AttendEmployeeMonth.jumpAddPage();

    });

    // 导出excel
    $('#btnExp').click(function () {
        AttendEmployeeMonth.exportExcel();
    });

    // 工具条点击事件
    table.on('tool(' + AttendEmployeeMonth.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'edit') {
            AttendEmployeeMonth.jumpEditPage(data);
        }
        if (layEvent === 'delete') {
            AttendEmployeeMonth.onDeleteItem(data);
        }
        if (layEvent === 'synchronousName') {
            AttendEmployeeMonth.onSynchronousNameItem(data);
        }



    });
    //初始化页面
    $(function () {
        var attendMonth = $("#month").val();
        AttendEmployeeMonth.getCreateAttendButton();
        setTimeout(() =>{
            AttendEmployeeMonth.search();
        },100);

    });
});
